package cuilitang.springcloudprovider.controller;

import cuilitang.springcloudprovider.pojo.User;
import cuilitang.springcloudprovider.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("getUserById")
    @ResponseBody
    public User getUserById(Integer id) {
       return userService.getUserById( id);
    }

    @PostMapping("getUserById")
    @ResponseBody
    public User getUserById2(Integer id) {
        return userService.getUserById( id);
    }
    @GetMapping("user/{id}")
    @ResponseBody
    public User getUserById3(@PathVariable("id") Integer id) {
        return userService.getUserById( id);
    }



    @GetMapping("/all")
    public String all(ModelMap model) {
        // 查询用户
        List<User> users = this.userService.queryAll();
        // 放入模型
        model.addAttribute("users", users);
        // 返回模板名称（就是classpath:/templates/目录下的html文件名）
        return "users";
    }
}
